﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class MapGridView : MonoBehaviour, IPointerClickHandler
{

    public int MapGridPosX;
    public int MapGridPoxY;

    private GameObject MapManager;
    private Image mapGridImage;
    private Vector2 startPos;
    public MapGridState currenGridState = MapGridState.normal;
    public MapGridState LastGridState = MapGridState.normal;

    void Awake()
    {
        mapGridImage = this.transform.GetComponent<Image>();
        MapManager = GameObject.FindGameObjectWithTag("MapManager");
        startPos = new Vector2(-320, -440);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        TeamManager._instance.PlayerFoods[PlayerAttack._instance.MainAttack].SetGoPos(MapGridPosX, MapGridPoxY);
        MapManager.GetComponent<MapManager>().ClearMapGridState();
        MapManager.GetComponent<MapManager>().ShowRange(TeamManager._instance.PlayerFoods[PlayerAttack._instance.MainAttack]);
    }

    //void Update()
    //{
    //    if (Input.GetMouseButtonDown(0))
    //        Debug.Log("2222222222222222222");
    //}


    /// <summary>
    /// 初始化显示地图格子的位置
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void Init(int x, int y)
    {
        MapGridPosX = x;
        MapGridPoxY = y;
        transform.SetParent(MapManager.transform);
        mapGridImage.rectTransform.localPosition = new Vector3(startPos.x + x * 80f, startPos.y + y * 80f, 0f);
        mapGridImage.transform.localScale = new Vector3(1, 1, 1);
        SetState(currenGridState);
    }

    /// <summary>
    /// 设置图片的状态
    /// </summary>
    /// <param name="State"></param>
    public void SetState(MapGridState State)
    {
        switch (State)
        {
            case MapGridState.normal:
                mapGridImage.color = Color.clear;
                break;
            case MapGridState.green:
                mapGridImage.color = new Color(0f, 255f, 0f, 0.8f);
                break;
            case MapGridState.red:
                mapGridImage.color = new Color(255f, 0f, 0f, 0.8f);
                break;
            case MapGridState.gray:
                mapGridImage.color = new Color(255f, 255f, 255f, 0.8f);
                break;
        }
        currenGridState = State;
    }
}


public enum MapGridState
{
    normal,
    green,
    red,
    gray
}